package com.xidian.zhiancloud.service.ypp.messageCenter.information;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.InformationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.Information;
import com.xidian.zhiancloud.model.InformationExample;
import com.xidian.zhiancloud.model.Notice;
import com.xidian.zhiancloud.model.syset.JobList;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.standard.expression.Each;

import java.util.List;

@Service
public class InformationService {
    @Autowired
    private InformationMapper informationMapper;

    public List<Information> getById(String status) {
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andStatusEqualTo(status);
        List<Information> informationList = informationMapper.selectByExample(informationExample);
        if (informationList == null) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        return informationList;
    }

    public void updateByIds(List<Information> informationList) {
        for (Information information:informationList){
            Information information2 = new Information();
            BeanUtils.copyProperties(information,information2);
            information2.setStatus("1");
            int result = informationMapper.updateByPrimaryKey(information2);
            if (result == 0) {
                throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
            }
        }

    }

    public Object getInformation(Long id) {
        Information information = informationMapper.selectByPrimaryKey(id);
        if (information == null){
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        
        if(information.getStatus().equals("0")) {
        	Information information2 = new Information();
            information2.setStatus("1");
            information2.setId(id);
            int result = informationMapper.updateByPrimaryKeySelective(information2);
        }
 
        return information;
    }

	public Page<Information> getInformationList(Information info) {
		// TODO Auto-generated method stub
		PageHelper.startPage(info.getPageNum(), info.getPageSize());
		Page<Information> pagedata =informationMapper.getInformationList(info);
		
		return pagedata;
	}

	public void deleteInformationeByIds(Long[] ids) {
		// TODO Auto-generated method stub
		informationMapper.deleteInformationeByIds(ids);
	}

	public void deleteInformationeAll() {
		// TODO Auto-generated method stub
		informationMapper.deleteInformationeAll();
	}

	public void updateInformationByIds(Long[] ids) {
		// TODO Auto-generated method stub
		informationMapper.updateInformationByIds(ids);
	}

	public void insertInfo(Information info) {
		// TODO Auto-generated method stub
		informationMapper.insertInfo(info);
	}
}
